﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace HiViLicenseManage
{
	public class KeyGenerator
	{
		public static List<char> _characters;
		public static Random _random = new Random();

		public static void SetRandomData(bool upper, bool lower, bool number)
		{
			_characters = new List<char>();
			if (upper == true) // Fill character list with A-Z.
				for (int i = 65; i <= 90; i++)
				{
					_characters.Add((char)i);
				}
			if (lower == true) // Fill character list with a-z.
				for (int i = 97; i <= 122; i++)
				{
					_characters.Add((char)i);
				}
			if (number == true) // Fill character list with 0-9.
				for (int i = 48; i <= 57; i++)
				{
					_characters.Add((char)i);
				}
		}
		public static string Generate(int lenght)
		{
			StringBuilder buffer = new StringBuilder(lenght);
			for (int i = 0; i < lenght; i++)
			{
				int randomNumber = _random.Next(0, _characters.Count);
				char randomChar = _characters[randomNumber];
				buffer.Append(randomChar);
			}
			return buffer.ToString();
		}
	}
}
